/*
* CourseCollectServiceImpl.java
* Created on  2014-5-15 下午4:46
* 版本       修改时间          作者      修改内容
* V1.0.1    2014-5-15       gaoxinyu    初始版本
*
*/
package com.genghis.ptas.course.service.impl;

import com.genghis.ptas.course.dao.CourseCollectDao;
import com.genghis.ptas.course.entity.CourseCollect;
import com.genghis.ptas.course.entity.CourseInfo;
import com.genghis.ptas.course.service.CourseCollectService;
import com.genghis.ptas.course.service.CourseStatisticsService;
import com.genghis.ptas.security.dao.UserDao;
import com.genghis.ptas.statistic.entity.CourseStatistics;
import com.genghis.ptas.util.DateUtil;
import com.genghis.ptas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类的描述信息
 *
 * @author fengxuanzhen
 * @version 1.0.1
 */
@Service("courseCollectService")
public class CourseCollectServiceImpl implements CourseCollectService {

    @Autowired
    private CourseCollectDao courseCollectDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private CourseStatisticsService courseStatisticsService;

    @Override
    public int addCourseCollectService(CourseCollect courseCollect) {
        //修改统计表
        CourseStatistics courseStatistics = courseStatisticsService.findCourseStatisticsByCourseId(courseCollect.getCourseId());
        courseStatistics.setCollectNumber(courseStatistics.getCollectNumber() + 1);
        courseStatisticsService.editCourseStatistics(courseStatistics);
        //进增
        int userId = userDao.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        courseCollect.setUserId(userId);
        courseCollect.setCollectDate(DateUtil.getSystemDate("yyyy-MM-dd HH:mm:ss"));
        return courseCollectDao.addCourseCollectService(courseCollect);
    }

    @Override
    public List<CourseInfo> findCourseCollectByUserId(Integer userId) {
        return courseCollectDao.findCourseCollectByUserId(userId);
    }

    @Override
    public Boolean judgeWhetherCollectByCourseId(CourseCollect courseCollect) {
        Integer userId = userDao.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        List<CourseCollect> courseCollectList = courseCollectDao.findCourseCollectByUserIdAndCourseId(userId, courseCollect.getCourseId());
        return courseCollectList.size() == 0;
    }
}
